﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gmis.IDAL;
using Gmis.DBUtility;


namespace Gmis.SqlServerDAL
{
    /// <summary>
    /// 数据访问类:MassOrg
    /// </summary>
    public partial class MassOrg : IMassOrg
    {
        public MassOrg()
        { }

        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from MassOrg");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Gmis.Model.MassOrg model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into MassOrg(");
            strSql.Append("sId,sRelationId,sType,sPosition,sPicture,sComment,sNumber)");
            strSql.Append(" values (");
            strSql.Append("@sId,@sRelationId,@sType,@sPosition,@sPicture,@sComment,@sNumber)");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50),
					new SqlParameter("@sRelationId", SqlDbType.VarChar,50),
					new SqlParameter("@sType", SqlDbType.NVarChar,50),
					new SqlParameter("@sPosition", SqlDbType.NVarChar,50),
					new SqlParameter("@sPicture", SqlDbType.NVarChar,128),
					new SqlParameter("@sComment", SqlDbType.NVarChar,300),
                    new SqlParameter("@sNumber", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.sId;
            parameters[1].Value = model.sRelationId;
            parameters[2].Value = model.sType;
            parameters[3].Value = model.sPosition;
            parameters[4].Value = model.sPicture;
            parameters[5].Value = model.sComment;
            parameters[6].Value = model.sNumber;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gmis.Model.MassOrg model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update MassOrg set ");
            strSql.Append("sRelationId=@sRelationId,");
            strSql.Append("sType=@sType,");
            strSql.Append("sPosition=@sPosition,");
            strSql.Append("sPicture=@sPicture,");
            strSql.Append("sComment=@sComment,");
            strSql.Append("sNumber=@sNumber");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sRelationId", SqlDbType.VarChar,50),
					new SqlParameter("@sType", SqlDbType.NVarChar,50),
					new SqlParameter("@sPosition", SqlDbType.NVarChar,50),
					new SqlParameter("@sPicture", SqlDbType.NVarChar,128),
					new SqlParameter("@sComment", SqlDbType.NVarChar,300),
                    new SqlParameter("@sNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = model.sRelationId;
            parameters[1].Value = model.sType;
            parameters[2].Value = model.sPosition;
            parameters[3].Value = model.sPicture;
            parameters[4].Value = model.sComment;
            parameters[5].Value = model.sNumber;
            parameters[6].Value = model.sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from MassOrg ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string sIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from MassOrg ");
            strSql.Append(" where sId in (" + sIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gmis.Model.MassOrg GetModel(string sId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 m.sId,sNumber,sRelationId,sType,sPosition,sPicture,m.sComment,sName,sSex,sNation,sEducation,sIdentity,dBorn,nAge,sNative,sPolitics,sMarry,sMobile,sAddress,sLive ");
            strSql.Append(" from MassOrg as m inner join Population as p on m.sRelationId = p.sId ");
            strSql.Append(" where m.sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            Gmis.Model.MassOrg model = new Gmis.Model.MassOrg();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sId"] != null && ds.Tables[0].Rows[0]["sId"].ToString() != "")
                {
                    model.sId = ds.Tables[0].Rows[0]["sId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sNumber"] != null && ds.Tables[0].Rows[0]["sNumber"].ToString() != "")
                {
                    model.sNumber = ds.Tables[0].Rows[0]["sNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sRelationId"] != null && ds.Tables[0].Rows[0]["sRelationId"].ToString() != "")
                {
                    model.sRelationId = ds.Tables[0].Rows[0]["sRelationId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sType"] != null && ds.Tables[0].Rows[0]["sType"].ToString() != "")
                {
                    model.sType = ds.Tables[0].Rows[0]["sType"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sPosition"] != null && ds.Tables[0].Rows[0]["sPosition"].ToString() != "")
                {
                    model.sPosition = ds.Tables[0].Rows[0]["sPosition"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sPicture"] != null && ds.Tables[0].Rows[0]["sPicture"].ToString() != "")
                {
                    model.sPicture = ds.Tables[0].Rows[0]["sPicture"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sComment"] != null && ds.Tables[0].Rows[0]["sComment"].ToString() != "")
                {
                    model.sComment = ds.Tables[0].Rows[0]["sComment"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sName"] != null && ds.Tables[0].Rows[0]["sName"].ToString() != "")
                {
                    model.sName = ds.Tables[0].Rows[0]["sName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sSex"] != null && ds.Tables[0].Rows[0]["sSex"].ToString() != "")
                {
                    model.sSex = ds.Tables[0].Rows[0]["sSex"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sNation"] != null && ds.Tables[0].Rows[0]["sNation"].ToString() != "")
                {
                    model.sNation = ds.Tables[0].Rows[0]["sNation"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sEducation"] != null && ds.Tables[0].Rows[0]["sEducation"].ToString() != "")
                {
                    model.sEducation = ds.Tables[0].Rows[0]["sEducation"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sIdentity"] != null && ds.Tables[0].Rows[0]["sIdentity"].ToString() != "")
                {
                    model.sIdentity = ds.Tables[0].Rows[0]["sIdentity"].ToString();
                }
                if (ds.Tables[0].Rows[0]["dBorn"] != null && ds.Tables[0].Rows[0]["dBorn"].ToString() != "")
                {
                    model.dBorn = DateTime.Parse(ds.Tables[0].Rows[0]["dBorn"].ToString());
                }
                if (ds.Tables[0].Rows[0]["nAge"] != null && ds.Tables[0].Rows[0]["nAge"].ToString() != "")
                {
                    model.nAge = int.Parse(ds.Tables[0].Rows[0]["nAge"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sNative"] != null && ds.Tables[0].Rows[0]["sNative"].ToString() != "")
                {
                    model.sNative = ds.Tables[0].Rows[0]["sNative"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sPolitics"] != null && ds.Tables[0].Rows[0]["sPolitics"].ToString() != "")
                {
                    model.sPolitics = ds.Tables[0].Rows[0]["sPolitics"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sMarry"] != null && ds.Tables[0].Rows[0]["sMarry"].ToString() != "")
                {
                    model.sMarry = ds.Tables[0].Rows[0]["sMarry"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sMobile"] != null && ds.Tables[0].Rows[0]["sMobile"].ToString() != "")
                {
                    model.sMobile = ds.Tables[0].Rows[0]["sMobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sAddress"] != null && ds.Tables[0].Rows[0]["sAddress"].ToString() != "")
                {
                    model.sAddress = ds.Tables[0].Rows[0]["sAddress"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sLive"] != null && ds.Tables[0].Rows[0]["sLive"].ToString() != "")
                {
                    model.sLive = ds.Tables[0].Rows[0]["sLive"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select m.sId,sNumber,sRelationId,sType,sPosition,sPicture,m.sComment,sName,sSex,sNation,sEducation,sIdentity,dBorn,nAge,sNative,sPolitics,sMarry,sMobile,sAddress,sLive ");
            strSql.Append(" FROM MassOrg as m inner join Population as p on m.sRelationId = p.sId ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" m.sId,sNumber,sRelationId,sType,sPosition,sPicture,m.sComment,sName,sSex,sNation,sEducation,sIdentity,dBorn,nAge,sNative,sPolitics,sMarry,sMobile,sAddress,sLive ");
            strSql.Append(" FROM MassOrg as m inner join Population as p on m.sRelationId = p.sId ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "MassOrg";
            parameters[1].Value = "sId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

